package server.handler.userHandler;

import com.alibaba.fastjson.JSONObject;
import common.EnvelopeMessage;
import common.bodies.NotificationBody;
import server.dao.FriendDao;
import server.dao.GroupDao;
import server.handler.SimpleHandler;
import server.session.Session;


import java.util.ArrayList;
import java.util.List;

public class NotificationListHandler implements SimpleHandler {
    @Override
    public void handle(EnvelopeMessage msg, Session session) {
        JSONObject body = msg.getBody();
        Integer userId = body.getInteger("userId");
        List<NotificationBody> requests = new ArrayList<>();
        List<NotificationBody> friendRequestList= new FriendDao().friendRequestList(userId);
        List<NotificationBody> groupRequestList = new GroupDao().groupRequestList(userId);
        requests.addAll(friendRequestList);
        requests.addAll(groupRequestList);
        JSONObject object =new JSONObject();
        object.put("requests",requests);
        EnvelopeMessage back = new EnvelopeMessage("notification","list",object);
        String json = JSONObject.toJSONString(back);
        System.out.println("服务端返回"+json);
        session.send(json);
    }
}
